package belf.migrate.api.table.schema;

import belf.migrate.api.model.QualifiersModel;
import belf.migrate.api.util.FieldCase;
import belf.migrate.api.util.QualifiedType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.io.Serializable;

@Getter
@EqualsAndHashCode
@RequiredArgsConstructor
public final class TableIdentifier implements Serializable {
    private static final long serialVersionUID = 1L;

    private final String catalogName;

    private final String databaseName;

    private final String schemaName;

    private final String tableName;

    private final QualifiersModel qualifiersModel;

    public static TableIdentifier of(String catalogName, String databaseName, String tableName, QualifiersModel qualifiersModel) {
        return new TableIdentifier(catalogName, databaseName, null, tableName, qualifiersModel);
    }

    public static TableIdentifier of(String catalogName, TablePath tablePath) {
        return new TableIdentifier(
                catalogName,
                tablePath.getDatabaseName(),
                tablePath.getSchemaName(),
                tablePath.getTableName(),
                tablePath.getQualifiersModel());
    }

    public static TableIdentifier of(
            String catalogName, String databaseName, String schemaName, String tableName, QualifiersModel qualifiersModel) {
        return new TableIdentifier(catalogName, databaseName, schemaName, tableName, qualifiersModel);
    }

    public TablePath toTablePath() {
        return TablePath.of(databaseName, schemaName, tableName, qualifiersModel);
    }

    public TableIdentifier copy() {
        return TableIdentifier.of(catalogName, databaseName, schemaName, tableName, qualifiersModel);
    }

    @Override
    public String toString() {
        if (schemaName == null) {
            return String.join(".", catalogName, databaseName, tableName);
        }
        return String.join(".", catalogName, databaseName, schemaName, tableName);
    }
}
